<template>
  <div class="community-intro">
    <div class="decoration-top">
      <div class="decoration-circle circle-1"></div>
      <div class="decoration-circle circle-2"></div>
      <div class="decoration-circle circle-3"></div>
    </div>
    
    <div class="page-header">
      <div class="header-content">
        <h2>社区基本信息</h2>
        <p class="header-subtitle">查看和管理您的社区详细信息</p>
      </div>
      <button v-if="!loading" @click="editCommunity" class="edit-btn">
        <svg viewBox="0 0 24 24" width="16" height="16" fill="currentColor">
          <path d="M3 17.25V21h3.75L17.81 9.94l-3.75-3.75L3 17.25zM20.71 7.04c.39-.39.39-1.02 0-1.41l-2.34-2.34c-.39-.39-1.02-.39-1.41 0l-1.83 1.83 3.75 3.75 1.83-1.83z"/>
        </svg>
        编辑
      </button>
    </div>

    <div class="intro-content">
      <div v-if="loading" class="loading-card">
        <div class="spinner"></div>
        <p>正在加载社区信息...</p>
      </div>
      
      <div v-else class="info-card">
        <div class="card-header">
          <div class="header-icon">
            <svg viewBox="0 0 24 24" width="24" height="24" fill="currentColor">
              <path d="M12 5.69l5 4.5V18h-2v-6H9v6H7v-7.81l5-4.5M12 3L2 12h3v8h6v-6h2v6h6v-8h3L12 3z"/>
            </svg>
          </div>
          <h3>基本信息</h3>
        </div>
        
        <div class="info-grid">
          <div class="info-item">
            <div class="item-icon">
              <svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor">
                <path d="M12 5.69l5 4.5V18h-2v-6H9v6H7v-7.81l5-4.5M12 3L2 12h3v8h6v-6h2v6h6v-8h3L12 3z"/>
              </svg>
            </div>
            <div class="item-content">
              <span class="label">社区名称</span>
              <span class="value">{{ communityInfo.name || '暂无' }}</span>
            </div>
          </div>
          
          <div class="info-item">
            <div class="item-icon">
              <svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor">
                <path d="M12 2C8.13 2 5 5.13 5 9c0 5.25 7 13 7 13s7-7.75 7-13c0-3.87-3.13-7-7-7zm0 9.5c-1.38 0-2.5-1.12-2.5-2.5s1.12-2.5 2.5-2.5 2.5 1.12 2.5 2.5-1.12 2.5-2.5 2.5z"/>
              </svg>
            </div>
            <div class="item-content">
              <span class="label">社区地址</span>
              <span class="value">{{ communityInfo.address || '暂无' }}</span>
            </div>
          </div>
          
          <div class="info-item">
            <div class="item-icon">
              <svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor">
                <path d="M12 2c1.1 0 2 .9 2 2 0 .74-.4 1.38-1 1.73v.77c0 .28.22.5.5.5s.5-.22.5-.5v-.27c.74.35 1.2 1.19 1.2 2.09 0 1.1-.9 2-2 2s-2-.9-2-2c0-.9.46-1.74 1.2-2.09v-.27c0-.28.22-.5.5-.5s.5.22.5.5v.27c-.6-.35-1-.99-1-1.73 0-1.1.9-2 2-2zm0-2C8.14 0 5 3.14 5 7c0 5.25 7 13 7 13s7-7.75 7-13c0-3.86-3.14-7-7-7zm0 9.5c-1.38 0-2.5-1.12-2.5-2.5s1.12-2.5 2.5-2.5 2.5 1.12 2.5 2.5-1.12 2.5-2.5 2.5z"/>
              </svg>
            </div>
            <div class="item-content">
              <span class="label">老年人数量</span>
              <span class="value">{{ communityInfo.elderCount || 0 }} 人</span>
            </div>
          </div>
          
          <div class="info-item">
            <div class="item-icon">
              <svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor">
                <path d="M12 2c1.1 0 2 .9 2 2 0 .74-.4 1.38-1 1.73v.77c0 .28.22.5.5.5s.5-.22.5-.5v-.27c.74.35 1.2 1.19 1.2 2.09 0 1.1-.9 2-2 2s-2-.9-2-2c0-.9.46-1.74 1.2-2.09v-.27c0-.28.22-.5.5-.5s.5.22.5.5v.27c-.6-.35-1-.99-1-1.73 0-1.1.9-2 2-2zm0-2C8.14 0 5 3.14 5 7c0 5.25 7 13 7 13s7-7.75 7-13c0-3.86-3.14-7-7-7zm0 9.5c-1.38 0-2.5-1.12-2.5-2.5s1.12-2.5 2.5-2.5 2.5 1.12 2.5 2.5-1.12 2.5-2.5 2.5z"/>
              </svg>
            </div>
            <div class="item-content">
              <span class="label">联系人</span>
              <span class="value">{{ communityInfo.contactPerson || '暂无' }}</span>
            </div>
          </div>
          
          <div class="info-item">
            <div class="item-icon">
              <svg viewBox="0 0 24 24" width="20" height="20" fill="currentColor">
                <path d="M6.62 10.79c1.44 2.83 3.76 5.14 6.59 6.59l2.2-2.2c.27-.27.67-.36 1.02-.24 1.12.37 2.33.57 3.57.57.55 0 1 .45 1 1V20c0 .55-.45 1-1 1-9.39 0-17-7.61-17-17 0-.55.45-1 1-1h3.5c.55 0 1 .45 1 1 0 1.25.2 2.45.57 3.57.11.35.03.74-.25 1.02l-2.2 2.2z"/>
              </svg>
            </div>
            <div class="item-content">
              <span class="label">联系电话</span>
              <span class="value">{{ communityInfo.contactPhone || '暂无' }}</span>
            </div>
          </div>
        </div>
      </div>
      
      <div v-if="!loading" class="description-card">
        <div class="card-header">
          <div class="header-icon">
            <svg viewBox="0 0 24 24" width="24" height="24" fill="currentColor">
              <path d="M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z"/>
            </svg>
          </div>
          <h3>社区简介</h3>
        </div>
        
        <div class="description-content">
          <p v-if="communityInfo.description">{{ communityInfo.description }}</p>
          <p v-else class="no-description">暂无社区简介</p>
          <div v-if="communityInfo.description_photo" class="image-container">
            <img :src="communityInfo.description_photo" alt="社区简介图片" class="description-image" />
          </div>
        </div>
      </div>
      
      <div v-if="!loading" class="announcement-card">
        <div class="card-header">
          <div class="header-icon">
            <svg viewBox="0 0 24 24" width="24" height="24" fill="currentColor">
              <path d="M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z"/>
            </svg>
          </div>
          <h3>社区公告</h3>
        </div>
        
        <div class="description-content">
          <p v-if="communityInfo.announcement">{{ communityInfo.announcement }}</p>
          <p v-else class="no-description">暂无社区公告</p>
          <div v-if="communityInfo.announcement_photo" class="image-container">
            <img :src="communityInfo.announcement_photo" alt="社区公告图片" class="description-image" />
          </div>
        </div>
      </div>
    </div>
    
    <!-- 编辑社区信息的模态框 -->
    <div v-if="showEditModal" class="modal-overlay" @click="closeEditModal">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h3>编辑社区信息</h3>
          <button class="close-btn" @click="closeEditModal">&times;</button>
        </div>
        <div class="modal-body">
          <div class="form-group">
            <label>社区名称:</label>
            <input v-model="editForm.name" placeholder="请输入社区名称" />
          </div>
          <div class="form-group">
            <label>社区地址:</label>
            <input v-model="editForm.address" placeholder="请输入社区地址" />
          </div>
          <div class="form-group">
            <label>联系人:</label>
            <input v-model="editForm.contactPerson" placeholder="请输入联系人" />
          </div>
          <div class="form-group">
            <label>联系电话:</label>
            <input v-model="editForm.contactPhone" placeholder="请输入联系电话" />
          </div>
          <div class="form-group">
            <label>社区简介:</label>
            <textarea v-model="editForm.description" placeholder="请输入社区简介"></textarea>
          </div>
          <div class="form-group">
            <label>社区简介图片:</label>
            <input type="file" @change="handleDescriptionPhotoUpload" accept="image/*" />
            <div v-if="editForm.description_photo" class="image-preview">
              <img :src="editForm.description_photo" alt="社区简介图片预览" class="preview-image" />
              <button type="button" @click="removeDescriptionPhoto" class="remove-image-btn">移除图片</button>
            </div>
          </div>
          <div class="form-group">
            <label>社区公告:</label>
            <textarea v-model="editForm.announcement" placeholder="请输入社区公告"></textarea>
          </div>
          <div class="form-group">
            <label>社区公告图片:</label>
            <input type="file" @change="handleAnnouncementPhotoUpload" accept="image/*" />
            <div v-if="editForm.announcement_photo" class="image-preview">
              <img :src="editForm.announcement_photo" alt="社区公告图片预览" class="preview-image" />
              <button type="button" @click="removeAnnouncementPhoto" class="remove-image-btn">移除图片</button>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button @click="submitEdit" class="submit-btn">保存</button>
          <button @click="closeEditModal" class="cancel-btn">取消</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import request from '../../utils/request'

const loading = ref(true)
const showEditModal = ref(false)
const communityInfo = ref({
  communityId: '',
  name: '',
  address: '',
  contactPerson: '',
  contactPhone: '',
  elderCount: 0,
  description: '',
  description_photo: '',
  announcement: '',
  announcement_photo: ''
})

const editForm = ref({
  communityId: '',
  name: '',
  address: '',
  contactPerson: '',
  contactPhone: '',
  description: '',
  description_photo: '',
  announcement: '',
  announcement_photo: ''
})

const getCommunityInfo = async () => {
  loading.value = true
  try {
    const res = await request.get('/community/get')
    if (res.code === 1) {
      communityInfo.value = res.data
      console.log('社区信息:', res.data)
      // 存储社区ID到localStorage，供其他页面使用
      localStorage.setItem('communityId', res.data.communityId)
      console.log('社区信息加载成功:', res.data)
    }
  } catch (error) {
    console.error('获取社区信息失败:', error)
  } finally {
    loading.value = false
  }
}

// 编辑社区信息
const editCommunity = () => {
  editForm.value = { ...communityInfo.value }
  showEditModal.value = true
}

// 关闭编辑模态框
const closeEditModal = () => {
  showEditModal.value = false
}

// 处理社区简介图片上传
const handleDescriptionPhotoUpload = async (event) => {
  const file = event.target.files[0]
  if (!file) return

  const formData = new FormData()
  formData.append('file', file)

  try {
    const res = await request.post('/common/upload', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
    if (res.code === 1) {
      editForm.value.description_photo = res.data
    } else {
      alert('图片上传失败: ' + res.message)
    }
  } catch (error) {
    console.error('上传图片失败:', error)
    alert('图片上传失败，请稍后重试')
  }
}

// 处理社区公告图片上传
const handleAnnouncementPhotoUpload = async (event) => {
  const file = event.target.files[0]
  if (!file) return

  const formData = new FormData()
  formData.append('file', file)

  try {
    const res = await request.post('/common/upload', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
    if (res.code === 1) {
      editForm.value.announcement_photo = res.data
    } else {
      alert('图片上传失败: ' + res.message)
    }
  } catch (error) {
    console.error('上传图片失败:', error)
    alert('图片上传失败，请稍后重试')
  }
}

// 移除社区简介图片
const removeDescriptionPhoto = () => {
  editForm.value.description_photo = ''
  // 重置文件输入框
  const fileInputs = document.querySelectorAll('input[type="file"]')
  if (fileInputs.length > 0) {
    fileInputs[0].value = ''
  }
}

// 移除社区公告图片
const removeAnnouncementPhoto = () => {
  editForm.value.announcement_photo = ''
  // 重置文件输入框
  const fileInputs = document.querySelectorAll('input[type="file"]')
  if (fileInputs.length > 1) {
    fileInputs[1].value = ''
  }
}

// 提交编辑
const submitEdit = async () => {
  try {
    const res = await request.put('/community/update', editForm.value)
    if (res.code === 1) {
      // 更新成功后刷新数据
      await getCommunityInfo()
      closeEditModal()
    } else {
      alert('更新失败: ' + res.message)
    }
  } catch (error) {
    console.error('更新社区信息失败:', error)
    alert('更新失败，请稍后重试')
  }
}

onMounted(() => {
  getCommunityInfo()
})
</script>

<style scoped>
.community-intro {
  padding: 24px;
  background: linear-gradient(135deg, #f0f7ff 0%, #e6f2ff 100%);
  min-height: 100%;
  position: relative;
  overflow: hidden;
}

.decoration-top {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 0;
}

.decoration-circle {
  position: absolute;
  border-radius: 50%;
  opacity: 0.1;
}

.circle-1 {
  width: 200px;
  height: 200px;
  background: #4facfe;
  top: -80px;
  right: -80px;
}

.circle-2 {
  width: 150px;
  height: 150px;
  background: #ff9a9e;
  bottom: 20%;
  left: -50px;
}

.circle-3 {
  width: 100px;
  height: 100px;
  background: #a1c4fd;
  top: 30%;
  right: 10%;
}

.page-header {
  margin-bottom: 24px;
  background: #fff;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: relative;
  z-index: 1;
}

.header-content {
  flex: 1;
}

.page-header h2 {
  margin: 0 0 8px;
  font-size: 24px;
  color: #2c3e50;
  font-weight: 600;
}

.header-subtitle {
  margin: 0;
  color: #7f8c8d;
  font-size: 14px;
}

.edit-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  background: #4facfe;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  transition: background 0.3s;
  z-index: 1;
}

.edit-btn:hover {
  background: #3a9bef;
}

.intro-content {
  display: flex;
  flex-direction: column;
  gap: 24px;
  position: relative;
  z-index: 1;
}

.loading-card {
  background: #fff;
  border-radius: 12px;
  padding: 40px;
  text-align: center;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}

.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #e9ecef;
  border-top: 4px solid #4facfe;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto 16px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.loading-card p {
  margin: 0;
  color: #6c757d;
  font-size: 16px;
}

.info-card {
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  overflow: hidden;
}

.card-header {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  color: white;
  padding: 20px 24px;
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-icon {
  width: 24px;
  height: 24px;
}

.card-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 500;
}

.info-grid {
  padding: 24px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
}

.info-item {
  display: flex;
  align-items: flex-start;
  gap: 16px;
  padding: 16px;
  border-radius: 8px;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.info-item::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 4px;
  height: 100%;
  background: linear-gradient(to bottom, #4facfe, #00f2fe);
}

.info-item:hover {
  background: #f8f9fa;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.info-item:hover::before {
  width: 6px;
}

.item-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: #e3f2fd;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #4facfe;
  flex-shrink: 0;
  z-index: 1;
}

.item-content {
  flex: 1;
  z-index: 1;
}

.label {
  display: block;
  color: #7f8c8d;
  font-size: 14px;
  margin-bottom: 4px;
  font-weight: 500;
}

.value {
  display: block;
  color: #2c3e50;
  font-size: 16px;
  font-weight: 500;
}

.description-card,
.announcement-card {
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  overflow: hidden;
  position: relative;
}

.description-card::after,
.announcement-card::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 4px;
}

.description-card::after {
  background: linear-gradient(90deg, #ff9a9e, #fad0c4);
}

.announcement-card::after {
  background: linear-gradient(90deg, #a1c4fd, #c2e9fb);
}

.description-card .card-header {
  background: linear-gradient(135deg, #ff9a9e 0%, #fad0c4 100%);
}

.announcement-card .card-header {
  background: linear-gradient(135deg, #a1c4fd 0%, #c2e9fb 100%);
}

.description-content,
.announcement-content {
  padding: 24px;
  display: flex;
  gap: 20px; /* 控制文字与图片之间的间距 */
}

.description-content p,
.announcement-content p {
  margin: 0;
  color: #2c3e50;
  font-size: 16px;
  line-height: 1.7;
  white-space: pre-wrap;
  flex: 1; /* 文字区域自动扩展 */
}

.no-description {
  color: #95a5a6 !important;
  font-style: italic;
}

.image-container {
  flex-shrink: 0; /* 图片不缩小 */
  max-width: 300px; /* 控制图片最大宽度 */
}

.description-image {
  max-width: 100%;
  max-height: 300px;
  border-radius: 8px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.1);
}
/* 模态框样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background: white;
  border-radius: 8px;
  width: 90%;
  max-width: 600px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  max-height: 90vh;
  overflow-y: auto;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 24px;
  border-bottom: 1px solid #e9ecef;
}

.modal-header h3 {
  margin: 0;
  color: #2c3e50;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: #aaa;
}

.close-btn:hover {
  color: #333;
}

.modal-body {
  padding: 24px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 6px;
  font-weight: 500;
  color: #495057;
}

.form-group input,
.form-group textarea {
  width: 100%;
  padding: 10px;
  border: 1px solid #ced4da;
  border-radius: 4px;
  font-size: 14px;
  box-sizing: border-box;
}

.form-group input[type="file"] {
  padding: 8px;
}

.form-group textarea {
  min-height: 100px;
  resize: vertical;
}

.image-preview {
  margin-top: 10px;
}

.preview-image {
  max-width: 200px;
  max-height: 200px;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  display: block;
  margin-bottom: 10px;
}

.remove-image-btn {
  background-color: #dc3545;
  color: white;
  border: none;
  padding: 5px 10px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
}

.remove-image-btn:hover {
  background-color: #bd2130;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 16px 24px;
  border-top: 1px solid #e9ecef;
}

.submit-btn,
.cancel-btn {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
}

.submit-btn {
  background-color: #28a745;
  color: white;
}

.submit-btn:hover {
  background-color: #218838;
}

.cancel-btn {
  background-color: #6c757d;
  color: white;
}

.cancel-btn:hover {
  background-color: #5a6268;
}

@media (max-width: 768px) {
  .community-intro {
    padding: 16px;
  }
  
  .info-grid {
    grid-template-columns: 1fr;
  }
  
  .page-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }
  
  .page-header h2 {
    font-size: 20px;
  }
  
  .info-item {
    padding: 12px;
  }
  
  .modal-content {
    width: 95%;
  }
  
  .decoration-circle {
    opacity: 0.05;
  }
  
  .circle-1 {
    width: 150px;
    height: 150px;
  }
  
  .circle-2 {
    width: 100px;
    height: 100px;
  }
  
  .circle-3 {
    width: 70px;
    height: 70px;
  }
  
  .preview-image {
    max-width: 150px;
    max-height: 150px;
  }
}
</style>